<?php

namespace app\services\product\product;

use app\dao\product\product\StoreProductLabelDao;
use app\model\product\product\StoreProduct;
use app\model\product\product\StoreProductLabel;
use app\model\product\sku\StoreProductLabelRelation;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;

class StoreProductLabelServices extends BaseServices
{
    public function __construct(StoreProductLabelDao $dao)
    {
        $this->dao = $dao;
    }

    public function getList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $order = 'sort_order desc';
        $list = $this->dao->getList($where, $page, $limit, $order);

        return [
            'list'  =>  $list->items(),
            'count' =>  $list->count()
        ];
    }

    public function all()
    {
        return StoreProductLabel::where('is_del', 0)->order('sort_order','desc')->field('id,label_name')->select()->toArray();
    }


    public function getHomeShow()
    {
        $labels = $this->dao->getList(['is_displayed_on_home'=>1, 'is_del' => 0], 1, 2, 'sort_order desc');

        $list = [];
        foreach ($labels as $label) {
            $list[] = [
                'id'    =>  $label['id'],
                'title' =>  $label['label_name']
            ];
        }
        array_unshift($list, [
            'id'    =>  0,
            'title' =>  '全部'
        ]);
        $list[] = [
            'id' => -1,
            'title' => '人气'
        ];

        foreach ($list as $k => &$item) {
            if ($k == 0) $item['sub_title'] = '猜你喜欢';
            if ($k == 1) $item['sub_title'] = '新奇口味';
            if ($k == 2) $item['sub_title'] = '冰爽尽享';
            if ($k == 3) $item['sub_title'] = '大家再买';
        }

        return $list;
    }

    public function getHomeProductList($label_id, $sort_type = 0, $sort_value = 0)
    {

        $query = StoreProduct::with(['defaultSku', 'label'])->when($label_id, function ($query) use ($label_id) {
            $query->whereFindInSet('label_id', $label_id);
        })->where('is_del', 0)->where('is_show', 1)
            ->where('publish_time', '<=', time());


        $sort_value = ($sort_value == 1) ? 'desc' : 'asc';

        if ($sort_type == 0) {
        }
        if ($sort_type == 1) { // 价格
            $query->where('price', $sort_value);
        }
        if ($sort_type == 2) {
            $query->where('sales', $sort_value);
        }
        if ($sort_value == -1) { // 人气
            $query->order('view_count', 'desc');
        }

        $products = $query->paginate();

        $storeProductServices = app()->make(StoreProductServices::class);

        return
            ['list' => $storeProductServices->filterList($products),
            'count' =>  $products->count()];
    }

    public function save(int $id, array $data)
    {

        if (!isset($data['label_name']) || $data['label_name'] == '') throw new AdminException(30006);

        if ($id) {
            $res = $this->dao->update($id, $data);
            if (!$res) throw new AdminException(10022);
        } else {
            $data['add_time'] = time();
            $res = $this->dao->save($data);
            if (!$res) throw new AdminException(10022);
        }

    }


    /**
     * 删除
     * @param int $id
     */
    public function del(int $id)
    {
        $res = $this->dao->update($id, ['is_del' => 1]);
        if (!$res) throw new AdminException(100008);
    }

}